package com.lingtianyu.tech.erbaoshen.core.context;

import java.util.HashMap;
import java.util.Map;

public class ErBaoShenRpcResultContext {
    private static final ThreadLocal<Map<String, Object>> RPC_RESULT_CONTEXT_THREADLOCAL = new ThreadLocal<>();

    public static void setAttachment(String key, Object value) {
        Map<String, Object> stringObjectMap = RPC_RESULT_CONTEXT_THREADLOCAL.get();
        if (stringObjectMap == null) {
            stringObjectMap = new HashMap<>();
            RPC_RESULT_CONTEXT_THREADLOCAL.set(stringObjectMap);
        }
        stringObjectMap.put(key, value);
    }

    public static Object getAttachment(String key) {
        Map<String, Object> stringObjectMap = RPC_RESULT_CONTEXT_THREADLOCAL.get();
        if (stringObjectMap == null) {
            return null;
        }
        return stringObjectMap.get(key);
    }

    public static void setAttachments(Map<String, Object> attachments) {
        if (attachments != null) {
            RPC_RESULT_CONTEXT_THREADLOCAL.set(attachments);
        }
    }

    public static Map<String, Object> getAttachments() {
        return RPC_RESULT_CONTEXT_THREADLOCAL.get();
    }

    public static void clear() {
        RPC_RESULT_CONTEXT_THREADLOCAL.remove();
    }
}
